﻿using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.PICCNursingConsent
{
    public partial class AddConsent : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        string Id = string.Empty;
        #endregion

        public AddConsent(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            this.LoginUser = loginUser;
            this.PatientInfo = patientInfo;
            BindInfo(Id);
        }
        public AddConsent(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            BindInfo(Id);
        }

        #region  保存
        private void simplebaocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dateriqi.Text))
                dateriqi.DateTime = DateTime.Now;
            WrittenConsent wc = new WrittenConsent();
            //PICC适应症
            wc.PICCShiYingZheng = (checkEditwaizhou.Checked ? 1 : 0) + "#" + (checkEditteshu.Checked ? 1 : 0) + "#" + (checkEditshuye.Checked ? 1 : 0) + "#" +
                                  (checkEditjiating.Checked ? 1 : 0) + "#" + (checkEditqita.Checked ? 1 : 0);
            //PICC适应症文本框
            wc.PICCShiYingZhengWenBenKuang = textEditqitawenbenkuang.Text;
            //置管过程
            wc.ZhiGuanGuoCheng = (checkEditzhiguan1.Checked ? 1 : 0) + "#" + (checkEditzhiguan2.Checked ? 1 : 0) + "#" + (checkEditzhiguan3.Checked ? 1 : 0) + "#" +
                                 (checkEditzhiguan4.Checked ? 1 : 0);
            //使用过程
            wc.ShiYongGuoCheng = (checkEdit1shiyong1.Checked ? 1 : 0) + "#" + (checkEdit1shiyong2.Checked ? 1 : 0) + "#" + (checkEdit1shiyong3.Checked ? 1 : 0) + "#" +
                                 (checkEdit1shiyong4.Checked ? 1 : 0) + "#" + (checkEdit1shiyong5.Checked ? 1 : 0) + "#" + (checkEdit1shiyong6.Checked ? 1 : 0) + "#" +
                                 (checkEdit1shiyong7.Checked ? 1 : 0) + "#" + (checkEdit1shiyong8.Checked ? 1 : 0);
            //患儿监护人意见
            wc.HuanErJianHuRenYiJian = texthuanerjianhurenyijian.Text;
            //签名
            wc.QianMing = textqianming.Text;
            //签名时间
            if (!string.IsNullOrEmpty(dateEditqianmingshjijian.Text))
                wc.QingMingShiJian = dateEditqianmingshjijian.DateTime.ToString("yyyy-MM-dd HH:mm:ss");
            //患者授权委托人签字
            wc.HuanZheShouQuanWeiTuoRenYiJian = texthzsqwtrqz.Text;
            //与患者关系
            wc.YuHuanGuanXi = textyuhuanzheguanxi.Text;
            //委托人签名
            wc.WeiTuoRenQianMing = textweituorenqianming.Text;
            //委托人联系方式
            wc.WeiTuoRenQianMing = textweituorenqianming.Text;
            //委托人联系方式
            wc.WeiTuoRenLianXiFangShi = textweituorenlianxifangshi.Text;
            //签名时间2
            if (!string.IsNullOrEmpty(dateEditqianmingshjijian2.Text))
                wc.QingMingShiJian2 = dateEditqianmingshjijian2.DateTime.ToString("yyyy-MM-dd HH:mm:ss");
            //获得授权的医务人员签字
            wc.HuoDeShouQianDeYiWuRenYuanQiaMming = texthdsqdywryqz.Text;
            //签名时间3
            if (!string.IsNullOrEmpty(dateEditqianmingshjijian3.Text))
                wc.QingMingShiJian3 = dateEditqianmingshjijian3.DateTime.ToString("yyyy-MM-dd HH:mm:ss");
            //主治医师签名
            wc.ZhuZhiYiShiQianMing = textzhushiyishiqianming.Text;
            //签名时间4
            if (!string.IsNullOrEmpty(dateEditqianmingshjijian4.Text))
                wc.QingMingShiJian4 = dateEditqianmingshjijian4.DateTime.ToString("yyyy-MM-dd HH:mm:ss");
            //表单ID
            wc.FormId = (int)FormType.WrittenConsentNursing;
            //表单名字
            wc.FormName = EnumHelper.GetDescription(FormType.WrittenConsentNursing);
            //表单ID
            record.FormId = (int)FormType.WrittenConsentNursing;
            //表单名字
            record.FormName = EnumHelper.GetDescription(FormType.WrittenConsentNursing);
            //住院号
            record.PatientId = PatientInfo.PATIENT_ID;
            //住院次数
            record.VisitId = PatientInfo.VISIT_ID;
            //患者姓名
            record.PatientName = PatientInfo.PATIENT_NAME;
            //床号
            record.BedNo = PatientInfo.BED_NO;
            //病区名称
            record.AreaName = PatientInfo.NURSE_CELL_NAME;
            //当前科室ID
            record.DepartmentId = PatientInfo.DEPARTMENT_ID;
            //当前科室名称
            record.DepartmentName = PatientInfo.DEPARTMENT_NAME;
            //当前患者病区ID
            record.AreaId = PatientInfo.NURSE_CELL_CODE;
            //当前患者病区名称
            record.AreaName = PatientInfo.NURSE_CELL_NAME;
            record.NursingFormData = XmlUtil.ObjectToXML(wc, Encoding.UTF8);
            record.AssessmentDate = dateriqi.DateTime;
            record.Id = string.IsNullOrEmpty(Id) ? 0 : Convert.ToInt32(Id);
            var ret2 = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            MessageBox.Show(ret2.M);
            if (record.Id > 0) return;
            //新增成功后绑定数据
            this.Id = string.Empty;
            BindInfo(this.Id);
            this.DialogResult = DialogResult.OK;    //点击保存后，关闭页面
        }
        #endregion

        #region 查询
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            BindInfo(this.Id);
        }
        #endregion

        #region 打印
        private void simplequxiao_Click(object sender, EventArgs e)
        {
            ConsentPrints frm = new ConsentPrints(LoginUser, PatientInfo, this.Id);
            frm.Show();
        }
        #endregion


        #region 双击事件显示赋值
        public void BindInfo(string id)
        {
            var ret = formService.GetNursingFormRecordOrderByTime(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.WrittenConsentNursing, 1);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                dateriqi.DateTime = DateTime.Now;
                if (string.IsNullOrEmpty(id))
                    record = list[0];
                else
                    record = list.Find(p => p.Id == Convert.ToInt32(id));
                this.Id = record.Id.ToString();
                var NursingFormData = record.NursingFormData;
                dateriqi.DateTime = Convert.ToDateTime(record.AssessmentDate);
                WrittenConsent wc = new WrittenConsent();
                wc = XmlUtil.XMLToObject<WrittenConsent>(NursingFormData, Encoding.UTF8);
                //PICC适应症
                var PICCSYZ = wc.PICCShiYingZheng.Split('#');
                checkEditwaizhou.Checked = (PICCSYZ[0].Equals("1"));
                checkEditteshu.Checked = (PICCSYZ[1].Equals("1"));
                checkEditshuye.Checked = (PICCSYZ[2].Equals("1"));
                checkEditjiating.Checked = (PICCSYZ[3].Equals("1"));
                checkEditqita.Checked = (PICCSYZ[4].Equals("1"));
                //PICC适应症文本框
                textEditqitawenbenkuang.Text = wc.PICCShiYingZhengWenBenKuang;
                //置管过程
                var ZhiGuanGuoCheng = wc.ZhiGuanGuoCheng.Split('#');
                checkEditzhiguan1.Checked = (ZhiGuanGuoCheng[0].Equals("1"));
                checkEditzhiguan2.Checked = (ZhiGuanGuoCheng[1].Equals("1"));
                checkEditzhiguan3.Checked = (ZhiGuanGuoCheng[2].Equals("1"));
                checkEditzhiguan4.Checked = (ZhiGuanGuoCheng[3].Equals("1"));
                //使用过程
                var SYGC = wc.ShiYongGuoCheng.Split('#');
                checkEdit1shiyong1.Checked = (SYGC[0].Equals("1"));
                checkEdit1shiyong2.Checked = (SYGC[1].Equals("1"));
                checkEdit1shiyong3.Checked = (SYGC[2].Equals("1"));
                checkEdit1shiyong4.Checked = (SYGC[3].Equals("1"));
                checkEdit1shiyong5.Checked = (SYGC[4].Equals("1"));
                checkEdit1shiyong6.Checked = (SYGC[5].Equals("1"));
                checkEdit1shiyong7.Checked = (SYGC[6].Equals("1"));
                checkEdit1shiyong8.Checked = (SYGC[7].Equals("1"));
                //患儿监护人意见
                texthuanerjianhurenyijian.Text = wc.HuanErJianHuRenYiJian;
                //签名
                textqianming.Text = wc.QianMing;
                //签名时间
                dateEditqianmingshjijian.Text = wc.QingMingShiJian;
                //患者授权委托人意见
                texthzsqwtrqz.Text = wc.HuanZheShouQuanWeiTuoRenYiJian;
                //与患者关系
                textyuhuanzheguanxi.Text = wc.YuHuanGuanXi;
                //委托人签名
                textweituorenqianming.Text = wc.WeiTuoRenQianMing;
                //委托人联系方式
                textweituorenlianxifangshi.Text = wc.WeiTuoRenLianXiFangShi;
                //签名时间2
                dateEditqianmingshjijian2.Text = wc.QingMingShiJian2;
                //获得授权的医务人员签名
                texthdsqdywryqz.Text = wc.HuoDeShouQianDeYiWuRenYuanQiaMming;
                //签名时间3--年
                dateEditqianmingshjijian3.Text = wc.QingMingShiJian3;
                //主治医师签名
                textzhushiyishiqianming.Text = wc.ZhuZhiYiShiQianMing;
                //签名时间4
                dateEditqianmingshjijian4.Text = wc.QingMingShiJian4;
            }
            else if (ret.Code == 2) { this.Id.ToString(); return; } else { MessageBox.Show(ret.M); }
        }
        #endregion
    }
}